﻿#region Software License
/**************************************************************/
/*          Business of Software Consulting                   */
/*          FREE SOFTWARE LICENSE                             */
/**************************************************************/
/*  Directory Manifest Generator                              */
/*  by: Mike Cating                                           */
/*  © 2009 Business of Software Consulting                    */
/*                                                            */
/* BUSINESS OF SOFTWARE CONSULTING DISCLAIMS ALL WARRANTIES,  */
/* RESPONSIBILITIES, AND LIABILITIES ASSOCIATED WITH          */
/* USE OF THIS CODE IN ANY WAY, SHAPE, OR FORM                */
/* REGARDLESS HOW IMPLICIT, EXPLICIT, OR OBSCURE IT           */
/* IS. IF THERE IS ANYTHING QUESTIONABLE WITH REGARDS         */
/* TO THIS SOFTWARE BREAKING AND YOU GAIN A LOSS OF           */
/* ANY NATURE, WE ARE NOT THE RESPONSIBLE PARTY. USE          */
/* OF THIS SOFTWARE CREATES ACCEPTANCE OF THESE TERMS         */
/*                                                            */
/* USE OF THIS CODE MUST RETAIN ALL COPYRIGHT NOTICES         */
/* AND LICENSES (MEANING THIS TEXT).                          */
/*                                                            */
/**************************************************************/
#endregion

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Build.Utilities;
using Microsoft.Build.Framework;
using System.Globalization;
using System.Diagnostics.CodeAnalysis;
using System.Security.Permissions;

namespace BOSTools.DirectoryManifestGenerator
{
    public class DirectoryManifestTask : Task
    {
        private ITaskItem _targetDirectory;
        private ITaskItem _outputFile;

        [Required]
        public ITaskItem TargetDirectory
        {
            get { return _targetDirectory; }
            set { _targetDirectory = value; }
        }

        [Required]
        public ITaskItem OutputFile
        {
            get { return _outputFile; }
            set { _outputFile = value; }
        }


        [SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands"), SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
        public override bool Execute()
        {
            if (this.BuildEngine != null)
            {
                Log.LogMessageFromText("Generating Directory Manifest", MessageImportance.Normal);
            }
            try
            {
                DirectoryManifestController controller = new DirectoryManifestController(
                            _targetDirectory.ItemSpec, _outputFile.ItemSpec);

                controller.Process();

                if (this.BuildEngine != null)
                {
                    Log.LogMessageFromText(
                        string.Format(CultureInfo.InvariantCulture,
                            "Directory Manifest for {0} created at {1}", _targetDirectory.ItemSpec,
                            _outputFile.ItemSpec), MessageImportance.Normal);
                }
                return true;
            }
            catch (Exception ex)
            {
                if (this.BuildEngine != null)
                {
                    Log.LogError("Unable to generate Directory Manifest " + ex.Message, null);
                }
                return false;

            }

        }
    }
}
